<?php
namespace App\Http\Controllers\Api;

use App\Api\Helpers\MoveNumber;
use App\Api\Helpers\SendMessage;
use App\Http\Controllers\Controller;
use App\Models\MShopOrders;
use App\Models\MGoodsPlan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use EasyWeChat\Factory;
use App\Services\OSS;

/**
 * 订单
 *
 */
class CrontabController extends Controller {
    use MoveNumber;
    use SendMessage;
    /**
     * 查询移动运营商所有订单的状态
     *
     */
    public function query_all_order(){
        $code = 'UIP_CARD_STATECHANGE';
        $orders_info = $this->getOrderStatus($code);
        if ( $orders_info['code'] == 200 ) {
            foreach ($orders_info['data'] as $key=> $value){
                $where = [
                    'order_sn' => $value['outorderid'],
                    'status'   => 1
                ];
                $orders_infos = MShopOrders::where($where)->first();
                if ( $orders_infos ) {
                    switch ($value['channgestate']){
                        case 0:
                            // 用户取消订单
                            $update_data = [
                                'status'=> -1,
                                'is_normal'=> 1,
                                'cancel_reason'=> '用户取消订单',
                            ];
                            // 测试用
//                            $update_data = [
//                                'status'=> 2,
//                                'active_time'=> date('Y-m-d H:i:s', time()),
//                            ];
                            break;
                        case 3:
                            // 已发货
                            $update_data = [
                                'status'=> 1,
                            ];
                            break;
                        case 5:
                            // 退款成功
                            $update_data = [
                                'status'=> -1,
                                'is_normal'=> 1,
                                'cancel_reason'=> '用户已退款',
                            ];
                            break;
                        case 31:
                            // 激活超时
                            $update_data = [
                                'status'=> 3,
                                'is_normal'=> 1,
                                'cancel_reason'=> '激活超时',
                            ];
                            break;
                        case 6:
                            // 已激活
                            $update_data = [
                                'status'=> 2,
                                'active_time'=> date('Y-m-d H:i:s', time()),
                            ];
                            break;
                        case 4:
                            // 申请退款
                            $update_data = [
                                'status'=> 3,
                                'is_normal'=> 1,
                                'cancel_reason'=> '已申请退款',
                            ];
                            break;
                        default:
                            $update_data = [];
                    }
                    $res = MShopOrders::where($where)->update($update_data);
                    if ( $value['channgestate'] == 0 ) {
                        file_put_contents('/var/log/crontab_run.log', '返佣方法调用：'.$orders_infos->uid.$value['outorderid']."\r\n", FILE_APPEND);
                        $this->active_success($orders_infos);
                        systemCommissions($orders_infos->uid, $value['outorderid']);
                    }
                    if ( $res ) {
                        $this->syncOrderSuccess($value['changeid']); // 同步订单反馈
                        return '更新成功';die;
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            }
        } else {
            return '无信息更新';die;
        }
//        $where = [
//            'order_sn' => 'O19122010198101012',
//            'status'   => 3
//        ];
//        $orders_infos = MShopOrders::where($where)->first();
//        if ( $orders_infos ) {
//            $update_data = [
//                'status'=> 2,
//                'active_time'=> date('Y-m-d H:i:s', time()),
//            ];
//
//            $res = MShopOrders::where($where)->update($update_data);
//
//            file_put_contents('/var/log/crontab_run.log', '返佣方法调用：'.$orders_infos->uid.$where['order_sn']."\r\n", FILE_APPEND);
//            systemCommissions($orders_infos->uid, $where['order_sn']);
//            if ( $res ) {
//                return '更新成功';die;
//            } else {
//                return -1;die;
//            }
//        }

        return 1;die;
    }

    /**
     * 套餐查询写入表
     *
     */
    public function phoneMealDataMove(){
        // 获取所有套餐信息
        $code = 'UIP_CARD_QRYPACKAGE';
        $data = $this->getPhoneMeal($code);

        $insert_data = [];
        if( $data['code'] == 200 ){
            foreach ($data['data'] as $key=> $value) {
                $plan_info = MGoodsPlan::where(['planid'=> $value['id']])->first();
                if ( !$plan_info ) {
                    $insert_data[] = [
                        "planid" => $value['id'],
                        "planname" => $value['pageShowName'],
                        "plancity" => $value['optionalCity'],
                        "planprice" => $value['free'],
                        "type" => 1,
                    ];
                }
            }
            if ( !empty($insert_data) ) {
                $res = MGoodsPlan::insert($insert_data);
                return $res ? '数据插入成功'.$res: '数据插入失败'.$res;die;
            }
        }
        return 1;die;
    }

    /**
     * 激活成功
     *
     */
    public function active_success($order_info){
        $template_id = 'HhTNyyFNRCHLH8FKiiCXojuFC8v7VhKc2KtlcsCwo5k';
        $user_info = DB::table('users')->where(['id'=> $order_info->uid])->first();
        $data = [
            'first'    => '您好！您号码已成功激活，插入手机即可使用。',
            'keyword1' => $order_info->phone,
            'keyword2' => '激活成功',
            'remark' => '如有疑问，请联系客服',
        ];

        $this->sendActiveMessage($user_info->watchid, $template_id, $data);
        return true;
    }
}
